package com.ixoko.swagger.controller.guider;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ixoko.swagger.dto.BaseReqDto;
import com.ixoko.swagger.dto.BaseResDto;
import com.ixoko.swagger.req.*;
import com.ixoko.swagger.resp.ClientGuideAssessListRespDto;
import com.ixoko.swagger.resp.ClientGuideUpdateRespDto;
import com.ixoko.swagger.resp.MyClientGuideListRespDto;
import com.ixoko.swagger.util.HttpClientUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author luojie
 * @Date 2018/7/7
 */
@Api(description = "攻略控制器")
@RestController
@RequestMapping("/tourism/guide")
public class GuideController {

    @Value("${http.client.base.url}")
    private String baseUrl;


    @ApiOperation(value = "攻略单个详细信息获取接口", httpMethod = "GET")
    @RequestMapping(value = "/detail")
    public BaseResDto<ClientGuideUpdateRespDto> detail(Long id) throws IOException, ExecutionException, URISyntaxException {
        String url = baseUrl + "/guide/detail";

        HashMap<String, Long> map = new HashMap<>();
        map.put("id", id);
        String post = HttpClientUtil.post(url, JSONObject.toJSONString(map));
        BaseResDto<ClientGuideUpdateRespDto> resDto = JSONObject.parseObject(post, BaseResDto.class);
        resDto.setData(JSONObject.parseObject(JSONObject.parseObject(post).getString("data"), ClientGuideUpdateRespDto.class));

        return resDto;
    }

    @ApiOperation(value = "我的攻略列表接口", httpMethod = "GET")
    @RequestMapping(value = "/list")
    public BaseResDto<List<MyClientGuideListRespDto>> list(BaseReqDto reqDto) throws IOException, ExecutionException {
        String url = baseUrl + "/guide/list";
        String post = HttpClientUtil.post(url, JSON.toJSONString(reqDto));
        BaseResDto<List<MyClientGuideListRespDto>> resDto = JSONObject.parseObject(post, BaseResDto.class);
        resDto.setData(JSONObject.parseArray(JSONObject.parseObject(post).getString("data"), MyClientGuideListRespDto.class));

        return resDto;
    }

    @ApiOperation(value = "我收藏的攻略列表", httpMethod = "GET")
    @RequestMapping(value = "/collect/list")
    public BaseResDto<List<MyClientGuideListRespDto>> collectList(BaseReqDto reqDto) throws IOException, ExecutionException {
        String url = baseUrl + "/guide/collect/list";
        String post = HttpClientUtil.post(url, JSON.toJSONString(reqDto));
        BaseResDto<List<MyClientGuideListRespDto>> resDto = JSONObject.parseObject(post, BaseResDto.class);
        resDto.setData(JSONObject.parseArray(JSONObject.parseObject(post).getString("data"), MyClientGuideListRespDto.class));

        return resDto;
    }


    @ApiOperation(value = "保存接口 修改也是这个接口  修改多传一个id", httpMethod = "POST")
    @RequestMapping("/save")
    public String save(ClientGuideSaveReqDto reqDto) throws IOException, ExecutionException {
        String url = baseUrl + "/guide/save";

        String post = HttpClientUtil.post(url, JSONObject.toJSONString(reqDto));
        return post;
    }

    @ApiOperation(value = "用户收藏攻略接口", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "攻略id", required = true)
    @RequestMapping(value = "/collect")
    public String collect(HttpServletRequest request, Long id) throws IOException, ExecutionException {
        String url = baseUrl + "/guide/collect";
        HashMap<String, Long> map = new HashMap<>();
        map.put("id", id);
        String post = HttpClientUtil.post(url, JSONObject.toJSONString(map));
        return post;
    }

    @ApiOperation(value = "攻略删除接口", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "攻略id", required = true)
    @RequestMapping("/remove")
    public String remove(Long id) throws IOException, ExecutionException {
        String url = baseUrl + "/guide/remove";
        HashMap<String, Long> map = new HashMap<>(16);
        map.put("id", id);
        String post = HttpClientUtil.post(url, JSONObject.toJSONString(map));
        return post;
    }


    @ApiOperation(value = "攻略收藏移除接口", httpMethod = "POST")
    @ApiImplicitParam(name = "collectId", value = "攻略收藏id", required = true)
    @RequestMapping("/remove/collect")
    public String removeCollect(Long collectId) throws IOException, ExecutionException {
        String url = baseUrl + "/guide/remove/collect";
        HashMap<String, Long> map = new HashMap<>(16);
        map.put("collectId", collectId);
        String post = HttpClientUtil.post(url, JSONObject.toJSONString(map));
        return post;
    }

    @ApiOperation(value = "攻略分享接口 分享之后返回的data里面有分享url", httpMethod = "POST")
    @RequestMapping("/share")
    public String share(ClientGuideShareReqDto reqDto) throws IOException, ExecutionException {
        String url = baseUrl + "/guide/share";
        String post = HttpClientUtil.post(url, JSONObject.toJSONString(reqDto));
        return post;
    }


    @ApiOperation(value = "攻略点赞接口  就是小爱心接口", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "攻略id", required = true)
    @RequestMapping("/fav")
    public String fav(Long id) throws IOException, ExecutionException {
        String url = baseUrl + "/guide/fav";
        HashMap<String, Long> map = new HashMap<>(16);
        map.put("id", id);
        String post = HttpClientUtil.post(url, JSONObject.toJSONString(map));
        return post;
    }

    @ApiOperation(value = "攻略评论信息接口", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "攻略id", required = true)
    @RequestMapping("/releaseAssess")
    public String releaseAssess(ClientGuideAssessReqDto reqDto) throws IOException, ExecutionException {
        String url = baseUrl + "/guide/releaseAssess";
        String post = HttpClientUtil.post(url, JSONObject.toJSONString(reqDto));
        return post;
    }


    @ApiOperation(value = "攻略评论信息移除接口", httpMethod = "POST")
    @ApiImplicitParam(name = "assessId", value = "评论id", required = true)
    @RequestMapping("/removeAssess")
    public String favCancel(Long assessId) throws IOException, ExecutionException {
        String url = baseUrl + "/guide/removeAssess";
        HashMap<String, Long> map = new HashMap<>(16);
        map.put("assessId", assessId);
        String post = HttpClientUtil.post(url, JSONObject.toJSONString(map));
        return post;
    }

    @ApiOperation(value = "回复攻略评论接口", httpMethod = "POST")
    @RequestMapping("/replyAssess")
    public String favCancel(ClientGuideReplyAssessReqDto reqDto) throws IOException, ExecutionException {
        String url = baseUrl + "/guide/replyAssess";
        String post = HttpClientUtil.post(url, JSONObject.toJSONString(reqDto));
        return post;
    }

    @ApiOperation(value = "评论列表接口", httpMethod = "POST")
    @RequestMapping("/assess/list")
    public String assessList(ClientGuideAssessListReqDto reqDto) throws IOException, ExecutionException {
        String url = baseUrl + "/guide/assess/list";
        String post = HttpClientUtil.post(url, JSONObject.toJSONString(reqDto));
        BaseResDto<List<ClientGuideAssessListRespDto>> resDto = JSONObject.parseObject(post, BaseResDto.class);
        resDto.setData(JSONObject.parseArray(JSONObject.parseObject(post).getString("data"), ClientGuideAssessListRespDto.class));
        return post;
    }
}
